package wheel;

import java.util.ArrayList;

import wheel.Game;

public class Puzzle {
	private Phrase phrase;
	private StringBuilder answer = new StringBuilder("");  // holds the answer so far, with *'s
	private String[] words;
	private String guesses = "";    // No letters guessed yet
	private ArrayList<String> phrasesGuessed = new ArrayList<String>();
    private String phraseVowels = "";
    private String phraseConsonants = "";
    private String correctlyGuessedVowels = "";
    private String correctlyGuessedConsonants = "";
    private boolean moreVowels = true;
    private boolean moreConsonants = true;
	
	// Constructors

	public Puzzle(Phrase phrase) {
		this.phrase = phrase;
		this.phrase.setText(this.phrase.getText().toUpperCase());
		parsePhrase();
	}
	
	
	// Puzzle functions
	private void parsePhrase() {
		for (char letter : this.phrase.getText().toCharArray()) {
			if (Game.PUNCTUATION.indexOf(letter) != -1) this.answer.append(letter);
			else this.answer.append("*");
			if (Game.VOWELS.indexOf(letter) != -1) this.phraseVowels += letter;
			if (Game.CONSONANTS.indexOf(letter) != -1) this.phraseConsonants += letter;
		}
		this.words = this.phrase.getText().split(Game.SPLITTERS);
		phraseVowels = removeDuplicateChars(phraseVowels);
		phraseConsonants = removeDuplicateChars(phraseConsonants);
	}
	
	public int evaluateGuess(char letter) {
		letter = Character.toUpperCase(letter);
		if (guesses.indexOf(letter) != -1) {
			// It's been guessed already
            giveFeedback(-1, letter);
			return -1;
		}
		else {
			// Store guess
			this.guesses += letter;
			// Count occurrences
			int counter = 0;
			for (int i=0; i < phrase.getText().length(); i++ ) {
			    if (phrase.getText().charAt(i) == letter) {
			        counter++;
			        answer.setCharAt(i, letter);
			    } 
			}
			giveFeedback(counter, letter);
			if (counter >= 1) 
				{
				if (Game.CONSONANTS.indexOf(letter) != -1) {
					correctlyGuessedConsonants += letter;
					if (correctlyGuessedConsonants.length() == phraseConsonants.length()) 
						moreConsonants = false;
				}
				if (Game.VOWELS.indexOf(letter) != -1) {
					correctlyGuessedVowels += letter;
					if (correctlyGuessedVowels.length() == phraseVowels.length()) 
						moreVowels = false;
				}
			}
			return counter;
		}
	}
	
	public boolean evaluateSolution(String answer) {
		String strippedPhrase = phrase.getText().replace("?", "").replace("!", "");
		String strippedAnswer = answer.replace("?", "").replace("!", "").toUpperCase();
		if (strippedAnswer.equals(strippedPhrase)) return true;
		else return false;
	}
	

	public void displayAnswer() {
		System.out.println("\n" + answer);
	}
	
	private void giveFeedback(int lettersFound, char letter) {
		if (lettersFound == 1) {
			Game.makeSound("ding");
			System.out.printf("There is 1 %s.\n", letter);
		}
		else if (lettersFound == 0) {
			Game.makeSound("buzzer");
			System.out.printf("There are no %s's.\n", letter);
		}
		else if (lettersFound == -1) {
			Game.makeSound("buzzer");
			System.out.printf("Sorry, that was already guessed.\n");
		}
		else {
			for (int i = 1; i <= lettersFound; i++) Game.makeSound("ding");
			System.out.printf("There are %d %s's.\n", lettersFound, letter);
		}
	}

	private String removeDuplicateChars(String s) {
	    StringBuilder noDupes = new StringBuilder();
	    for (int i=0; i < s.length(); i++) {
	        String si = s.substring(i, i + 1);
	        if (noDupes.indexOf(si) == -1) {
	            noDupes.append(si);
	        }
	    }
	    return noDupes.toString();
	}
	
	// Getters and Setters
	public Phrase getPhrase() {
		return phrase;
	}
	public String[] getWords() {
		return words;
	}
	public void setWords(String[] words) {
		this.words = words;
	}
	public String getGuesses() {
		return guesses;
	}
	public void setGuesses(String guesses) {
		this.guesses = guesses;
	}
	public ArrayList<String> getPhrasesGuessed() {
		return phrasesGuessed;
	}
	public void addGuessedPhrase(String phraseGuessed) {
		this.phrasesGuessed.add(phraseGuessed);
	}
	public String getAnswer() {
		return answer.toString();
	}
	public String getPhraseVowels() {
		return phraseVowels;
	}
	public String getPhraseConsonants() {
		return phraseConsonants;
	}
	public String getCorrectlyGuessedVowels() {
		return correctlyGuessedVowels;
	}
	public String getCorrectlyGuessedConsonants() {
		return correctlyGuessedConsonants;
	}
	public boolean isMoreVowels() {
		return moreVowels;
	}
	public boolean isMoreConsonants() {
		return moreConsonants;
	}
}
